/*
 * fun [function] 需要防抖的函数
 * delay [number] 毫秒，防抖期限值
 */
function debounce(fun, delay = 300) {
  let timer;
  return function () {
    let ctx = this
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fun.apply(ctx, args)
    }, delay)
  }
}
/*
 * fun [function] 需要节流的函数
 * delay [number] 毫秒，节流期限值
 */
function throttle(fun, interval = 300) {
    let last;
    let timer;
    return function () {
        let ctx = this
        let args = arguments
        let now = new Date()
        if (last && now - last < interval) {
          clearTimeout(timer)
          timer = setTimeout(function () {
            last = now
          }, interval)
        } else {
          last = now
          fun.apply(ctx, args)
        }
    }
}
//   显示消息框
function toast(str,icon = 'none',delay = 1500){
  uni.showToast({
    title: str,
    icon,
    duration: delay,
    mask: true
  });
}
/**
 * 获取url参数（第一种）
 * @param {*} name
 * @param {*} origin
 */
function getQueryString(key) {
  let after =
    window.location.search.substr(1) ||
    window.location.hash.split("?")[1] ||
    null;
  if (after) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var r = after.match(reg);
    if (r != null) {
      return decodeURIComponent(r[2]);
    } else {
      return null;
    }
  } else {
    // console.log(11);
    return null;
  }
}

export {
  debounce,
  throttle,
  toast,
  getQueryString
}